package com.kfh.ybracelet.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.adapter.GroupAdapter;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.bean.GroupBean;
import com.kfh.ybracelet.bean.MessageBean;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 群聊列表页面
 * <p/>
 * Created by Aaron on 16/2/22.
 */
public class ChatGroupListActivity extends BaseActivity {

    private List<GroupBean.TopicListEntity> topicList = new ArrayList<>();
    private GroupAdapter adapter;
//    private Intent mIntent;
    private MessageBean contact;
    private int choosePostion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_group_list);

        initWidget();

        loadGroups();

        EventBus.getDefault().register(this);

    }

    /**
     * 获取群聊列表
     */
    private void loadGroups() {

        com.alibaba.fastjson.JSONObject js = new com.alibaba.fastjson.JSONObject();
        js.put("token", sp.getString(Constant.TOKEN, ""));

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT_TALK + Constant.GROUPLIST, js.toJSONString(), getClass().getName(),
                new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            GroupBean groupBean = JSONObject.parseObject(str, GroupBean.class);

//                            if (groupBean.code == Constant.STATUS_SUCCESS) {

                                topicList = groupBean.topicList;

                                if (adapter != null) {
                                    adapter.setTopicList(topicList);
                                    adapter.notifyDataSetChanged();
                                }

//                            } else {
//                                showToast(Constant.showErrorMsg(groupBean.code, ChatGroupListActivity.this));
//                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);

    }

    private void initWidget() {

        ListView lvgroups = (ListView) findViewById(R.id.lv_groups);
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.chat_group));

        iv_left.setOnClickListener(this);

        adapter = new GroupAdapter(this, topicList);
        lvgroups.setAdapter(adapter);

        lvgroups.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (topicList != null && topicList.get(position) != null) {

                    choosePostion = position;

                    Intent  mIntent = new Intent(ChatGroupListActivity.this, ChatActivity.class);
                    contact = new MessageBean();
//                    topic 字段, receiveName 字段, receivePic字段 , chatType 字段
                    contact.channelId = topicList.get(position).topic;
                    contact.receiveName = topicList.get(position).groupName;
                    contact.receivePic = topicList.get(position).groupImgUrl;
//                    contact.chatType = Constant.CHAT_GROUP;//群聊
                    mIntent.putExtra("contact", contact);
                    startActivity(mIntent);

                }

            }
        });

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());

        EventBus.getDefault().unregister(this);

    }

    /**
     * 退出群聊天 通知刷新群列表
     * @param mb
     */
    public void onEventMainThread(MessageBean mb) {

        switch (mb.operationStyle) {

            case Constant.OFF_GROUP://退出群

//                topicList.clear();
//
//                loadGroups();

                if (topicList!=null) {

                    topicList.remove(choosePostion);
                    adapter.notifyDataSetChanged();

                }

                break;

        }

    }


}
